package org.jeecg.modules.doctor;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.dic.service.IDicBeGoodAtService;
import org.jeecg.common.modules.doctor.entity.*;
import org.jeecg.common.modules.doctor.service.*;
import org.jeecg.common.modules.register.entity.RegisterPatient;
import org.jeecg.common.modules.register.service.IRegisterPatientService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.DySmsEnum;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.data.service.CommonService;
import org.jeecg.modules.data.service.DySendMsg;
import org.jeecg.modules.data.service.PageReturn;
import org.jeecg.modules.system.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* @Description: doctor_register_operate
* @Author: jeecg-boot
* @Date:   2020-12-18
* @Version: V1.0
*/
@Api(tags="doctor_register_operate")
@RestController
@RequestMapping("/doctorRegisterOperate/doctorRegisterOperate")
@Slf4j
public class DoctorRegisterOperateController extends JeecgController<DoctorRegisterOperate, IDoctorRegisterOperateService> {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private IDoctorRegisterOperateService doctorRegisterOperateService;

    @Resource
    private IDoctorRegisterService doctorRegisterService;
    @Resource
    private IDicBeGoodAtService dicBeGoodAtService;
    @Resource
    private IDoctorRegisterHospitalService doctorRegisterHospitalService;
    @Resource
    private IDoctorService doctorService;
    @Resource
    private IRegisterPatientService registerPatientService;
    @Resource
    private IDoctorRegisterRecordService doctorRegisterRecordService;
   /**
    * 分页列表查询
    *
    * @param doctorRegisterOperate
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "doctor_register_operate-分页列表查询")
   @ApiOperation(value="doctor_register_operate-分页列表查询", notes="doctor_register_operate-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(DoctorRegisterOperate doctorRegisterOperate,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       String name = req.getParameter("name");
       String phone = req.getParameter("phone");
       Integer status = null;
       String statusStr = req.getParameter("status");
       if(statusStr != null){
           status = Integer.valueOf(statusStr);
       }

       Page<DoctorRegisterOperate> pageList = new Page<>(pageNo,pageSize);
       pageList = doctorRegisterOperateService.queryOperate(pageList,name,phone,status);
       log.info("查询当前页："+pageList.getCurrent());
       log.info("查询当前页数量："+pageList.getSize());
       log.info("查询结果数量："+pageList.getRecords().size());
       log.info("数据总数："+pageList.getTotal());
       return Result.ok(pageList);
   }

   /**
    *   添加
    *
    * @param doctorRegisterOperate
    * @return
    */
   @AutoLog(value = "doctor_register_operate-添加")
   @ApiOperation(value="doctor_register_operate-添加", notes="doctor_register_operate-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody DoctorRegisterOperate doctorRegisterOperate) {
       doctorRegisterOperate.setCreateTime(new Date());
       boolean res = doctorRegisterOperateService.save(doctorRegisterOperate);
       if(res){
           return Result.ok("保存成功！");
       }else{
           return Result.error("保存失败！");
       }
   }
   
   /**
    *  编辑
    *
    * @param doctorRegisterOperate
    * @return
    */
   @AutoLog(value = "doctor_register_operate-编辑")
   @ApiOperation(value="doctor_register_operate-编辑", notes="doctor_register_operate-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody DoctorRegisterOperate doctorRegisterOperate) {
       doctorRegisterOperate.setUpdateTime(new Date());
       boolean res = doctorRegisterOperateService.updateById(doctorRegisterOperate);
       if(res){
           return Result.ok("保存成功！");
       }else{
           return Result.error("保存失败！");
       }
   }
   
   /**
    *   通过id删除
    *
    * @return
    */
   @AutoLog(value = "doctor_register_operate-通过id删除")
   @ApiOperation(value="doctor_register_operate-通过id删除", notes="doctor_register_operate-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       DoctorRegisterOperate doctorRegisterOperate = doctorRegisterOperateService.getById(id);
       doctorRegisterOperate.setDeleteTime(new Date());
       doctorRegisterOperate.setDeleteStatus(1);
       boolean res = doctorRegisterOperateService.updateById(doctorRegisterOperate);
       if(res){
           return Result.ok("删除成功！");
       }else{
           return Result.error("删除失败！");
       }
   }
   
   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "doctor_register_operate-批量删除")
   @ApiOperation(value="doctor_register_operate-批量删除", notes="doctor_register_operate-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.doctorRegisterOperateService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.ok("批量删除成功!");
   }

    /**
     * 运营者短信消息测试
     * @throws ClientException
     * @throws ParseException
     */
    @GetMapping(value = "/smsOperate")
    public void smsOperate() throws ClientException, ParseException {
        //组织挂号、挂号单信息
        DoctorRegister doctorRegister = doctorRegisterService.getById("1348879895710732290");
        log.info(String.valueOf(doctorRegister));
        DoctorRegisterRecord doctorRegisterRecord = doctorRegisterRecordService.getById("1348879896742531073");


        //运营人员列表（未被删除的运营人员）
        List<DoctorRegisterOperate> operateList = doctorRegisterOperateService.operateList();

        //处理挂号日期
        Date visitTime = new SimpleDateFormat("yyyy-MM-dd").parse(doctorRegister.getVisitTime());
        String visitTimeStr = new SimpleDateFormat("yyyy年MM月dd日").format(visitTime);

        //组织医院科室信息
        String depart = "";
        String departmentName = dicBeGoodAtService.getById(doctorRegister.getDepartment()).getName(); //科室名称
        String departEnd = departmentName.substring(departmentName.length() - 1); //“xxx科室”的最后一个字
        if(departEnd.equals("科")){
            depart = departmentName.substring(0,departmentName.length() - 1);  //去除"xxx科室"的最后一个字
        }else{
            depart = departmentName;
        }
        //医院名称
        String hospitalName = doctorRegisterHospitalService.getById(doctorRegister.getRegisterHospitalId()).getName();
        //医生信息
        Doctor doctor = doctorService.getById(doctorRegister.getDoctorId());
        //患者信息
        RegisterPatient registerPatient = registerPatientService.getById(doctorRegisterRecord.getPatientId());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name_1",registerPatient.getName());  //患者姓名
        jsonObject.put("hospital_ment",hospitalName + " " +depart); //xxx医院xxx科
        jsonObject.put("name_title",doctor.getName()+"("+doctor.getTitle()+")");  //xx医生xx职位
        jsonObject.put("date_part",visitTimeStr + " " + doctorRegister.getVisitPart());   //xx时间xx号
        jsonObject.put("num",doctorRegisterRecord.getVisitNum());
        jsonObject.put("patient_p","电话："+registerPatient.getMobile());
        log.info(String.valueOf(jsonObject));
        try {
            //通知运营人员
            for(DoctorRegisterOperate doctorRegisterOperate:operateList){
                DySendMsg.sendSms(doctorRegisterOperate.getPhone(),jsonObject, DySmsEnum.REGISTER_OPERATE_NOTICE);
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
